/**
 * 
 */
package edu.umd.clip.lm.util;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;


public class ConstCountDistributionBinding extends TupleBinding<ConstCountDistribution> {
	public static final ConstCountDistributionBinding instance = new ConstCountDistributionBinding();
	
	private ConstCountDistributionBinding() {}
	
	/* (non-Javadoc)
	 * @see com.sleepycat.bind.tuple.TupleBinding#entryToObject(com.sleepycat.bind.tuple.TupleInput)
	 */
	@Override
	public ConstCountDistribution entryToObject(TupleInput input) {
		int numCounts = input.readPackedInt();
		long keys[] = new long[numCounts];
		long values[] = new long[numCounts];

		for(int i=0; i<numCounts; ++i) {
			long k = input.readPackedLong();
			keys[i] = k;
			long v = input.readPackedLong();
			values[i] = v;
		}
		return new ConstCountDistribution(keys, values);
	}

	/* (non-Javadoc)
	 * @see com.sleepycat.bind.tuple.TupleBinding#objectToEntry(java.lang.Object, com.sleepycat.bind.tuple.TupleOutput)
	 */
	@Override
	public void objectToEntry(ConstCountDistribution dist, TupleOutput output) {
		output.writePackedInt(dist.size());
		long keys[] = dist.keys();
		long values[] = dist.values();
		
		for(int i=0; i<keys.length; ++i) {
			output.writePackedLong(keys[i]);
			output.writePackedLong(values[i]);
		}
	}
	
}